<?php

namespace app\backstage\controller;

use think\Controller;
use think\Request;
use app\backstage\model\OverseasHouse as OverseasModel;
use app\api\model\Index as apiModel;
use Db;
class OverseasHouse extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request, OverseasModel $houseModel)
    {
        if($request->isAjax()){

            //分页
            $pageSize = 10;
            $params = $request->param();

            $page = $params['page']?:1;
            $offset = ($page-1)*$pageSize;

            $where = [];
            //搜索
            if(!empty($params['start'])){
                $where[] = ['a.create_time','>=',strtotime($params['start'])];
            }

            if(!empty($params['end'])){
                $where[] = ['a.create_time','<=',strtotime($params['end'])];
            }

            if(!empty($params['seach_text'])){
                $where[] = ['a.title','like',"%{$params['seach_text']}%"];
            }

            //楼盘列表
            $data = $houseModel->field('a.title,a.unit_price,a.total_area,a.total_price,a.create_time,a.id,a.thumb_img,b.shortname area_name')
                ->alias('a')->join('overseas_region b','a.area_id=b.id','left')
                ->where($where)->order('a.id','desc')->limit($offset,$pageSize)->select();

            if($data){
                foreach($data as &$v){
                    $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
                    $v['thumb_img'] = config('api.oss_img_path').$v['thumb_img'];

                }
            }

            if(!empty($where)){
                $count = count($data);
            }else{
                $count = $houseModel->count();
            }

            $pageData = ['count'=>$count,'page'=>$page,'pageSize'=>$pageSize];

            return ['code'=>200,'listData'=>$data,'pageData'=>$pageData];

        }
        return view('index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        $broker = Db::name('house_broker')->field('id,broker_name')->select();
        $labels = Db::name('house_label')->field('id,tag_name')->where('type',4)->select();

        $regions = Db::name('overseas_region')->alias('a')
            ->join('overseas_region b','a.pid=b.id','left')
            ->field('a.id,a.shortname,b.shortname parent')
            ->where('a.pid','>',0)->select();

        $res = array(
            'aspect'=>config('api.aspect'),
            'carport_types'=>config('api.carport_types'),
            'purpose'=>config('api.purpose'),
            'heating'=>config('api.heating'),
            'property_right_types'=>config('api.property_right_types'),
            'transaction_types'=>config('api.transaction_types'),
            'fitment_case'=>config('api.fitment_case'),
            'brokers'=>$broker,
            'labels' =>$labels,
            'regions' =>$regions,
        );
        return view('create',$res);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if($request->isAjax()){

            $data = $request->post();


            if(!empty($data['labels']) ){
                $data['labels'] = join(',',$data['labels']);
            }

//            $data['module_id'] = 2;


            $data['viewpager'] = json_encode($data['viewpager']);

            $data['create_time'] = time();

            $data['coding'] = 'H'.time();


            $model = new OverseasModel($data);

            $res = $model->save();

            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'插入失败'];
            }

        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $info = OverseasModel::where('id',$id)->find();

        $broker = Db::name('house_broker')->field('id,broker_name')->select();
        $labels = Db::name('house_label')->field('id,tag_name')->where('type',4)->select();

        $regions = Db::name('overseas_region')->alias('a')
            ->join('overseas_region b','a.pid=b.id','left')
            ->field('a.id,a.shortname,b.shortname parent')
            ->where('a.pid','>',0)->select();

        $info['labels'] = explode(',',$info['labels']);

        $res = array(
            'aspect'=>config('api.aspect'),
            'carport_types'=>config('api.carport_types'),
            'purpose'=>config('api.purpose'),
            'heating'=>config('api.heating'),
            'property_right_types'=>config('api.property_right_types'),
            'transaction_types'=>config('api.transaction_types'),
            'fitment_case'=>config('api.fitment_case'),
            'brokers'=>$broker,
            'labels' =>$labels,
            'regions' =>$regions,
            'details' =>$info
        );
        return view('edit',$res);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if($request->isAjax()){

            $data = $request->param();

            if(!empty($data['labels']) ){
                $data['labels'] = join(',',$data['labels']);
            }

            $data['viewpager'] = json_encode($data['viewpager']);


            $model = new OverseasModel();

            $res = $model->save($data,['id'=>$id]);

            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'修改失败'];
            }

        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = OverseasModel::destroy($id);

        if($res){
            return ['code'=>200];
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
    }


    /**
     * 批量删除
     *
     * @param  string  $ids
     * @return \think\Response
     */
    public function delete_all()
    {

        if($ids = request()->post('ids')){
            $res = OverseasModel::destroy($ids);

            if($res){
                return ['code'=>200];
            }else{
                return ['code'=>101,'msg'=>'删除失败'];
            }
        }else{
            return ['code'=>101,'msg'=>'缺少参数'];
        }

    }
}
